package com.lp.controller;

import com.lp.po.Argument;
import com.lp.po.PagingVO;
import com.lp.service.ArgumentService;
import com.lp.userException.CustomException;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.annotation.Resource;
import java.util.List;

@Controller
@RequestMapping("/admin/argument")
public class AdminArgumentController {

    @Resource
    private ArgumentService argumentService;

    @RequestMapping("/showArgument")
    public String showArgument(Model model, Integer page) throws Exception {
        List<Argument> list=null;
        //页码对象
        PagingVO pagingVO = new PagingVO();
        int argumentNumber=argumentService.getCount();
        //设置总页数
        pagingVO.setTotalCount(this.argumentService.getCount());
        if (page == null || page == 0) {
            pagingVO.setToPageNo(1);
            list = argumentService.findAllArgument(1);
        } else {
            pagingVO.setToPageNo(page);
            list = argumentService.findAllArgument(page);
        }
        model.addAttribute("pagingVO", pagingVO);
        model.addAttribute("argumentList",list);
        model.addAttribute("num",argumentNumber);
        return "/admin/showArgument";
    }

    @RequestMapping(value = "/selectArgument", method = {RequestMethod.POST})
    private String selectArgument(String name, Model model, Integer page) throws Exception {
        PagingVO pagingVO = new PagingVO();
        List<Argument> list=null;
        int argumentNumberByName=0;
        pagingVO.setTotalCount(this.argumentService.getCountByName('%'+name+'%'));
        if(name!=null){
            if (page == null || page == 0) {
                pagingVO.setToPageNo(1);
                list = argumentService.findByArgumentName(1,'%'+name+'%');
                argumentNumberByName=argumentService.getCountByName('%'+name+'%');
            } else {
                pagingVO.setToPageNo(page);
                list = argumentService.findByArgumentName(page,'%'+name+'%');
                argumentNumberByName=argumentService.getCountByName('%'+name+'%');
            }
            model.addAttribute("pagingVO", pagingVO);
            model.addAttribute("argumentList", list);
            model.addAttribute("num",argumentNumberByName);
            return "/admin/showArgument";
        }else{
            return "admin/showArgument";
        }
    }

    @RequestMapping(value = "/addArgument")
    public String addArgument() throws Exception {
        return "/admin/addArgument";
    }

    @RequestMapping(value = "/doAddArgument", method = {RequestMethod.POST})
    public String doAddArgument(Argument argument, Model model) throws Exception {
        //判断标题
        Boolean result = argumentService.save(argument);
        if (!result) {
            model.addAttribute("message", "标题重复");
            return "forward:/admin/argument/addArgument";
        }
        //重定向
        return "redirect:/admin/argument/showArgument";
    }

    @RequestMapping(value="editArgument")
    public String editArgument(Integer id, Model model) throws Exception{
        if (id == null) {
            return "redirect:/admin/argument/showArgument";
        }
        Argument argument = argumentService.findById(id);
        if (argument == null) {
            throw new CustomException("未找到参数");
        }
        model.addAttribute("argument", argument);
        return "/admin/editArgument";
    }

    @RequestMapping(value="doEditArgument",method = {RequestMethod.POST})
    public String doEditArgument(Argument argument) throws Exception{
        argumentService.updataById(argument.getAid(), argument);
        return "redirect:/admin/argument/showArgument";
    }

    @RequestMapping(value = "/removeArgument", method = {RequestMethod.GET})
    private String removeArgument(Integer id) throws Exception {
        if (id == null) {
            return "/admin/argument/showArgument";
        }
        argumentService.removeById(id);
        return "redirect:/admin/argument/showArgument";
    }
}
